package com.xianyu.us.service.impl;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xianyu.us.common.exception.ServiceException;
import com.xianyu.us.common.utils.ShiroUtils;
import com.xianyu.us.dao.UsGoodsDao;
import com.xianyu.us.dao.UsOrderDao;
import com.xianyu.us.pojo.entity.UsGoods;
import com.xianyu.us.pojo.entity.UsOrder;
import com.xianyu.us.pojo.entity.UsPayOrderParam;
import com.xianyu.us.service.UsOrderService;

/**
 * 订单支付成功后做存储操作
 * @author wangshili
 *
 */
@Service
public class UsOrderServiceImpl implements UsOrderService{

	@Autowired
	private UsOrderDao usOrderDao;
	
	@Autowired
	private UsGoodsDao usGoodsDao;
	
	@Transactional //涉及到两种表的操作，使用事务
	@Override
	public void insertOrder(UsPayOrderParam params) {
		System.out.println(params);
		UsOrder order = new UsOrder(); 
		order.setBuyerId(params.getBuyerId());//买家id
		order.setSellerId(params.getSellerId());//卖家id
		order.setOrderNo(params.getOut_trade_no());//交易单号
		order.setContent(params.getBody());//商品描述
		order.setNumber(Integer.valueOf(params.getNumber()));//数量
		order.setRecAddr(params.getRecAddr());//收货地址
		/**
		 * 以下内容需要查询商品信息
		 */
		UsGoods findByGoods = usGoodsDao.findGoodsById(params.getCommoditysId());//通过商品id查询商品
		order.setImgUrl(findByGoods.getImgUrl());
		order.setPrice(findByGoods.getPrice());
		order.setSendAddr(findByGoods.getSendAddr());
		
		Integer rows = usOrderDao.insertOrder(order);//插入数据
		//如果订单失败，支付宝会不断的重复发生该请求，直到成功
		if(rows<1)
			throw new ServiceException("订单添加失败，请重试");
	}

}
